$(function() {
	$(window).load(function(){
		$(".mCustomScrollbar").mCustomScrollbar({
			scrollButtons:{
			enable:true
			}, 
			scrollInertia : 500
		});
		
		
	});
	
	/* 4 14  추가 */
	
	/* 추가 */
		/* 동작안함. 수정요망
		$(".mCSB_container").bind("mousewheel",function(e){			
			var wheelDelta = e.originalEvent.wheelDelta;	
			if ( wheelDelta > 0) { 		
				$('.mCSB_container ul li').stop().animate({ "top": "+30px" }, 500,function(){
					$('.mCSB_container ul li').stop().animate({ "top": "0px" }, 500);
				});
				
			} else if ( wheelDelta < 0){
				$('.mCSB_container ul li').animate({ "bottom": "+30px" }, 500,function(){
					$('.mCSB_container ul li').stop().animate({ "bottom": "0px" }, 500);
				});
			}
		});
		*/
	$('span.all').hover(function(){
		$('span.all a').css('background','url(./images/bg_arr.png) no-repeat center 38px')
		
		$(".gnbPart>li>ul").stop().slideUp(100)
		$('.gnbAll').slideDown(300);
		});
	/*
	$('.gnbAll').mouseleave(function() {
		$('span.all a').css('background','none')
		$('.gnbAll').slideUp(300);		
	});
	*/
	$(".gnbPart>li>ul").hide()
	$(".gnbPart>li").hover(function(){
		$('.gnbmask').css('height','212px');
		$('.gnbAll').slideUp(300);
		$('span.all a').css('background','none')
		$(".gnbPart>li>ul").stop().slideUp(100);
		$(this).find('ul').stop().slideDown(100);		
	});
	
	
	/* 포커스이벤트 */
	$(".gnbPart>li").focusin(function(){
		$('.gnbAll').slideUp(300);
		$('span.all a').css('background','none')
		$('.gnbmask').css('height','212px');
		$(".gnbPart>li>ul").stop().slideUp(100);
		$(this).find('ul').stop().slideDown(100);
		
	})
	$(".gnbPart>li").focusout(function(){
		$('.gnbAll').slideUp(300);
		$('span.all a').css('background','none')
		$('.gnbmask').css('height','212px');
		$(".gnbPart>li>ul").stop().slideUp(100);
		$(this).find('ul').stop().slideDown(100);
		
	})
	
	$('.sub4').children('li').eq(2).focusin(function() {
		$('.gnbPart').stop().animate({ "left":"-262px"})
		$('.arrowRight').fadeIn(200);
		$('.arrowLeft').fadeOut(200);
	});
	$('.no_bg').focusin(function(){
		$('.arrowRight').click()
		});
		
	$('span.all').focusin(function(){
		$('span.all a').css('background','url(./images/bg_arr.png) no-repeat center 38px')
		$(".gnbPart>li>ul").stop().slideUp(100)
		$('.gnbAll').slideDown(300);
		});	
	$('span.all').focusout(function(){
		$(".gnbPart>li>ul").stop().slideUp(100)		
		$('span.all a').css('background','none')		
		});													
	
	/* slide gnb 화살표 이벤트 */
	$('.arrowRight').hide()
	$('.arrowLeft').click(function() {
		$('.gnbPart').stop().animate({ "left":"-262px"})
		$('.arrowRight').fadeIn(200);
		$('.arrowLeft').fadeOut(200);
	});
	
	$('.arrowRight').click(function() {
		$('.gnbPart').stop().animate({ "left":"0"})
		$('.arrowLeft').fadeIn(200);
		$('.arrowRight').fadeOut(200);
	});
	
	$('.contList').hover(function() {
		$(".gnbPart>li>ul").stop().slideUp(100)
		
		if ( $('.gnbmask').height('190') )
		{$('.gnbmask').stop().animate({'height':"70px"},50);}
	
		
	});
	
	contHeight();

	/* $(".login a").click(function(){
		$(".login").fadeOut(800);
		$(".logout").fadeIn(800);
		$(".head01").css("backgroundImage", "url(images/bg_header_on.png)");
	});

	$(".logout a").click(function(){
		$(".logout").fadeOut(800);
		$(".login").fadeIn(800);
		$(".head01").css("backgroundImage", "url(images/bg_header_off.png)");
	}); */		

	$(".mCustomScrollbar").mouseenter(function() {
		$(this).find(".mCSB_scrollTools").css({visibility:"visible"});
		var idx = $(this).index();
		if(idx==0){
			$(".bg_shadow").css({display:"none"});
			$(".bg_brown").css({display:"block"});
			$(".bg_box").css({display:"block"});
			$(".bg01").css({display:"block"});
			$(".br01").css({display:"none"});
			$(".bg_box").css({display:"none"});
		} else if(idx==1){
			$(".bg_shadow").css({display:"none"});
			$(".bg_brown").css({display:"block"});
			$(".bg_box").css({display:"block"});				
			$(".bg02").css({display:"block"});
			$(".br02").css({display:"none"});
			$(".bg_box").css({display:"none"});
		}
	});

	$(".contList li").mouseleave(function() {
		$(".bg_shadow").css({display:"none"});
		$(".bg_brown").css({display:"block"});
		$(".bg_box").css({display:"block"});
	});

	$(".contList li").mouseleave(function() {
		$(this).find(".mCSB_scrollTools").css({visibility:"hidden"});
	});	

	$(".card li").mouseenter(function() {
		var src = $(this).find("img").attr("src");
		$(this).find("img").attr("src",src.replace("_off","_on"));
	});

	$(".card li").mouseleave(function() {
		var src = $(this).find("img").attr("src");
		$(this).find("img").attr("src",src.replace("_on","_off"));
	});		

	$(".toggle li").click(function() {
		var idx = $(this).index();
		var barPos = 0;
		if(idx==1){barPos=0}else if(idx==2){barPos=110};

		if(idx!=0){
			if(!$(".open").hasClass("on")){
				if(idx!=1){
					$(".gnb").css({position:"relative",top:"0px"});
					$(".content").css({marginTop:"-5px"});
				}
			}

			if(idx==1){
				enable_scroll();
			}else{
				disable_scroll();
				$(".contWrap .cont").eq(2).children("img").css({display:"block"});
			}

			if(!$(this).hasClass("on")){
				$(".contWrap").stop().animate(
					{left:-(idx*1280)+"px"},
					{queue:true,duration:500,easing:"easeInOutExpo",
					complete : function() {
						$(".toggle li").removeClass("on");
						$(".toggle li").eq(idx).addClass("on");
						if(!$(".open").hasClass("on")){
							if(idx==1){
								$(".gnb").css({position:"fixed",top:"190px"});
								$(".content").css({marginTop:"75px"});
							}
						};
						if(idx==1){$(".contWrap .cont").eq(2).children("img").css({display:"none"});}
					}
				});
				$(".toggle span").stop().animate(
					{left:barPos+"px"},
					{queue:true,duration:500,easing:"easeInOutExpo"}
				);
			};
		};
		return false;
	});


	var keys = [37, 38, 39, 40];

	function preventDefault(e) {
	  e = e || window.event;
	  if (e.preventDefault)
	      e.preventDefault();
	  e.returnValue = false;  
	}

	function keydown(e) {
	    for (var i = keys.length; i--;) {
	        if (e.keyCode === keys[i]) {
	            preventDefault(e);
	            return;
	        }
	    }
	}

	function wheel(e) {
	  preventDefault(e);
	}

	function disable_scroll() {
	  if (window.addEventListener) {
	      window.addEventListener('DOMMouseScroll', wheel, false);
	  }
	  window.onmousewheel = document.onmousewheel = wheel;
	  document.onkeydown = keydown;
	}

	function enable_scroll() {
	    if (window.removeEventListener) {
	        window.removeEventListener('DOMMouseScroll', wheel, false);
	    }
	    window.onmousewheel = document.onmousewheel = document.onkeydown = null;  
	}

	$(".menuBanner li").click(function() {
		var idx = $(this).index();
		if(!$(this).hasClass("on")){
			if(idx!=0){
				$(".bg_banner li").fadeOut(800);
				$(".bg_banner li").eq(1).fadeIn(800);
				$(".menuBanner li").removeClass("on");
				$(this).addClass("on");
				menuOnOff(2);
			} else {
				$(".bg_banner li").fadeOut(800);
				$(".bg_banner li").eq(0).fadeIn(800);
				$(".menuBanner li").removeClass("on");
				$(this).addClass("on");	
				menuOnOff(0);				
			};
		};
		return false;
	});

	$(".open").click(function(){
		if($(this).hasClass("on")){
			$(".mainBanner").stop().animate(
				{height:"0px"},
				{queue:true,duration:500,easing:"easeInOutExpo",
				complete : function() {
					$(".open").removeClass("on");
				}
			});
			$(".gnb").css({position:"fixed",top:"550px"});
			$(".content").css({marginTop:"75px"});
			$(".gnb").stop().animate(
				{top:"190px"},
				{queue:true,duration:500,easing:"easeInOutExpo",
				complete : function() {
					$(".gnb").css({position:"fixed"});
				}
			});
		} else {
			$(".gnb").css({position:"relative",top:"0px"});
			$(".content").css({marginTop:"-3px"});
			$(".mainBanner").stop().animate(
				{height:"294px"},
				{queue:true,duration:500,easing:"easeInOutExpo",
				complete : function() {
					$(".open").addClass("on");
				}
			});		
		};
		return false;
	});

    $(".contList li").bind("mousewheel DOMMouseScroll", function(e) {
        var scrollTo = null;

        if (e.type == "mousewheel") {
            scrollTo = (e.originalEvent.wheelDelta * -1);
        }
        else if (e.type == "DOMMouseScroll") {
            scrollTo = 40 * e.originalEvent.detail;
        }

        if (scrollTo) {
            e.preventDefault();
            $(this).scrollTop(scrollTo + $(this).scrollTop());
        }		
    });

    $(".contList li").on("mousewheel DOMMouseScroll", function (e) {
        var scrollTo = null;

        if (e.type == "mousewheel") {
            scrollTo = (e.originalEvent.wheelDelta * -1);
        }
        else if (e.type == "DOMMouseScroll") {
            scrollTo = 40 * e.originalEvent.detail;
        }

        if (scrollTo) {
            e.preventDefault();
            $(this).scrollTop(scrollTo + $(this).scrollTop());
        }
    });
});


function menuOnOff(i){
	$(".menuBanner li").each(function() {
		var src = $(this).find("img").attr("src");
		$(this).find("img").attr("src",src.replace("_on","_off"));
	});
	var src1 = $(".menuBanner li").eq(i).find("img").attr("src");
	$(".menuBanner li").eq(i).find("img").attr("src",src1.replace("_off","_on"));
};

function contHeight(){
	$(".contList>li").css({height:"929px"});/* 20140415 modify KHW delete 1392px */
}


$(window).resize(function(){
	contHeight();
});

$(window).scroll(function(){
	scrollEvent();
});

function scrollEvent(){
	var s_top = $(window).scrollTop();
	if($(".open").hasClass("on")){
		if(s_top < 338){
			$(".content").css({marginTop:"0px"});
			$(".gnb").css({position:"relative",top:"0px"});
			$(".mainBanner").css({marginTop:s_top+"px",height:(294-s_top)+"px"});
			$("#header").css({height:"190px"});
			$(".head01").css({display:"block"});
			$(".head02").css({display:"none"});	
		} else if((s_top >= 338) && (s_top < 513)){
			$(".mainBanner").css({marginTop:"294px",height:"0px"});
			$(".gnb").css({position:"fixed",top:190-(s_top-338)+"px"});
			$(".content").css({marginTop:"70px"});
			$("#header").css({height:190-(s_top-338)+"px"});
			$(".head01").css({display:"block"});
			$(".head02").css({display:"none"});				
		} else if(s_top > 514){
			$(".gnb").css({position:"fixed", top:"38px"});
			$("#header").css({height:"38px"});
			$(".head01").css({display:"none"});
			$(".head02").css({display:"block"});
		}
	} else {
		if((s_top >= 0) && (s_top < 176)){
			$(".gnb").css({position:"fixed",top:190-(s_top)+"px"});
			$(".content").css({marginTop:"70px"});
			$("#header").css({height:190-(s_top)+"px"});
			$(".head01").css({display:"block"});
			$(".head02").css({display:"none"});				
		} else if(s_top >= 176){
			$(".gnb").css({top:"38px"});
			$("#header").css({height:"38px"});
			$(".head01").css({display:"none"});
			$(".head02").css({display:"block"});
		}
	}
	
	
		
	
};


/* func_select */
$(function() {
	$('.func_select>button').click(function() {
		$(this).next('ul').toggleClass('blind');
		return false;
	});
		
	$(document).click(function(event) {
		//console.log($(event.target).parent());
		if (!$(event.target).parent().is('.func_select')) {
			$('.func_select>ul').addClass('blind');
		}
	});
	
});
